package com.hrg.demo.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: zy
 * @Version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private Integer code;
//    private boolean success;
    private String msg;
    Object data;

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(200, message, data);
    }

    public static <T> Result<T> success(String message) {
        return new Result<>(200, message, null);
    }

    public static <T> Result<T> error(String message, T data) {
        return new Result<>(500, message, data);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(500, message, null);
    }

    public static <T> Result<T> response(ResultResponse response) {
        return new Result<>(response.getCode(), response.getMessage(), null);
    }

    public static <T> Result<T> response(ResultResponse response, T data) {
        return new Result<>(response.getCode(), response.getMessage(), data);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static Result parse(String result) {
        try {
            JSONObject parse = JSON.parseObject(result);
            return parse.toJavaObject(Result.class);
        } catch (Exception e) {
            return null;
        }
    }
}
